//
//  HPKnowledge.m
//
//  Created by 啊  on 16/4/24
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "HPKnowledge.h"


NSString *const kHPKnowledgeKType = @"kType";
NSString *const kHPKnowledgeKColor = @"kColor";
NSString *const kHPKnowledgeKContentUrl = @"kContentUrl";
NSString *const kHPKnowledgeKId = @"kId";
NSString *const kHPKnowledgeKTitle = @"kTitle";


@interface HPKnowledge ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation HPKnowledge

@synthesize kType = _kType;
@synthesize kColor = _kColor;
@synthesize kContentUrl = _kContentUrl;
@synthesize kId = _kId;
@synthesize kTitle = _kTitle;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.kType = [self objectOrNilForKey:kHPKnowledgeKType fromDictionary:dict];
            self.kColor = [self objectOrNilForKey:kHPKnowledgeKColor fromDictionary:dict];
            self.kContentUrl = [self objectOrNilForKey:kHPKnowledgeKContentUrl fromDictionary:dict];
            self.kId = [self objectOrNilForKey:kHPKnowledgeKId fromDictionary:dict];
            self.kTitle = [self objectOrNilForKey:kHPKnowledgeKTitle fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.kType forKey:kHPKnowledgeKType];
    [mutableDict setValue:self.kColor forKey:kHPKnowledgeKColor];
    [mutableDict setValue:self.kContentUrl forKey:kHPKnowledgeKContentUrl];
    [mutableDict setValue:self.kId forKey:kHPKnowledgeKId];
    [mutableDict setValue:self.kTitle forKey:kHPKnowledgeKTitle];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.kType = [aDecoder decodeObjectForKey:kHPKnowledgeKType];
    self.kColor = [aDecoder decodeObjectForKey:kHPKnowledgeKColor];
    self.kContentUrl = [aDecoder decodeObjectForKey:kHPKnowledgeKContentUrl];
    self.kId = [aDecoder decodeObjectForKey:kHPKnowledgeKId];
    self.kTitle = [aDecoder decodeObjectForKey:kHPKnowledgeKTitle];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_kType forKey:kHPKnowledgeKType];
    [aCoder encodeObject:_kColor forKey:kHPKnowledgeKColor];
    [aCoder encodeObject:_kContentUrl forKey:kHPKnowledgeKContentUrl];
    [aCoder encodeObject:_kId forKey:kHPKnowledgeKId];
    [aCoder encodeObject:_kTitle forKey:kHPKnowledgeKTitle];
}

- (id)copyWithZone:(NSZone *)zone
{
    HPKnowledge *copy = [[HPKnowledge alloc] init];
    
    if (copy) {

        copy.kType = [self.kType copyWithZone:zone];
        copy.kColor = [self.kColor copyWithZone:zone];
        copy.kContentUrl = [self.kContentUrl copyWithZone:zone];
        copy.kId = [self.kId copyWithZone:zone];
        copy.kTitle = [self.kTitle copyWithZone:zone];
    }
    
    return copy;
}


@end
